home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / camera.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  11.6 KB  |  574 lines

  1.  
  2. #include "qe3.h"
  3.  
  4. #define    PAGEFLIPS    2
  5.  
  6. void DrawPathLines (void);
  7.  
  8. camera_t    camera;
  9.  
  10. /*
  11. ============
  12. Cam_Init
  13. ============
  14. */
  15. void Cam_Init (void)
  16. {
  17. //    camera.draw_mode = cd_texture;
  18. //    camera.draw_mode = cd_solid;
  19. //    camera.draw_mode = cd_wire;
  20.  
  21.     camera.timing = false;
  22.  
  23.     camera.origin[0] = 0;
  24.     camera.origin[1] = 20;
  25.     camera.origin[2] = 46;
  26.  
  27.     camera.color[0] = 0.3;
  28.     camera.color[1] = 0.3;
  29.     camera.color[2] = 0.3;
  30. }
  31.  
  32.  
  33. //============================================================================
  34.  
  35. void Cam_BuildMatrix (void)
  36. {
  37.     float    xa, ya;
  38.     float    matrix[4][4];
  39.     int        i;
  40.  
  41.     xa = camera.angles[0]/180*Q_PI;
  42.     ya = camera.angles[1]/180*Q_PI;
  43.  
  44.     // the movement matrix is kept 2d
  45.  
  46.     camera.forward[0] = cos(ya);
  47.     camera.forward[1] = sin(ya);
  48.     camera.right[0] = camera.forward[1];
  49.     camera.right[1] = -camera.forward[0];
  50.  
  51.     glGetFloatv (GL_PROJECTION_MATRIX, &matrix[0][0]);
  52.  
  53.     for (i=0 ; i<3 ; i++)
  54.     {
  55.         camera.vright[i] = matrix[i][0];
  56.         camera.vup[i] = matrix[i][1];
  57.         camera.vpn[i] = matrix[i][2];
  58.     }
  59.  
  60.     VectorNormalize (camera.vright);
  61.     VectorNormalize (camera.vup);
  62.     VectorNormalize (camera.vpn);
  63. }
  64.  
  65. //===============================================
  66.  
  67. /*
  68. ===============
  69. Cam_ChangeFloor
  70. ===============
  71. */
  72. void Cam_ChangeFloor (qboolean up)
  73. {
  74.     brush_t    *b;
  75.     float    d, bestd, current;
  76.     vec3_t    start, dir;
  77.  
  78.     start[0] = camera.origin[0];
  79.     start[1] = camera.origin[1];
  80.     start[2] = 8192;
  81.     dir[0] = dir[1] = 0;
  82.     dir[2] = -1;
  83.  
  84.     current = 8192 - (camera.origin[2] - 48);
  85.     if (up)
  86.         bestd = 0;
  87.     else
  88.         bestd = 16384;
  89.  
  90.     for (b=active_brushes.next ; b != &active_brushes ; b=b->next)
  91.     {
  92.         if (!Brush_Ray (start, dir, b, &d))
  93.             continue;
  94.         if (up && d < current && d > bestd)
  95.             bestd = d;
  96.         if (!up && d > current && d < bestd)
  97.             bestd = d;
  98.     }
  99.  
  100.     if (bestd == 0 || bestd == 16384)
  101.         return;
  102.  
  103.     camera.origin[2] += current - bestd;
  104.     Sys_UpdateWindows (W_CAMERA|W_Z_OVERLAY);
  105. }
  106.  
  107.  
  108. //===============================================
  109.  
  110. int    cambuttonstate;
  111. static    int    buttonx, buttony;
  112. static    int    cursorx, cursory;
  113.  
  114. face_t    *side_select;
  115.  
  116. #define    ANGLE_SPEED    300
  117. #define    MOVE_SPEED    400
  118.  
  119. /*
  120. ================
  121. Cam_PositionDrag
  122. ================
  123. */
  124. void Cam_PositionDrag (void)
  125. {
  126.     int        x, y;
  127.  
  128.     Sys_GetCursorPos (&x, &y);
  129.     if (x != cursorx || y != cursory)
  130.     {
  131.         x -= cursorx;
  132.         VectorMA (camera.origin, x, camera.vright, camera.origin);
  133.         y -= cursory;
  134.         camera.origin[2] -= y;
  135.  
  136.         Sys_SetCursorPos (cursorx, cursory);
  137.         Sys_UpdateWindows (W_CAMERA | W_XY_OVERLAY);
  138.     }
  139. }
  140.  
  141. /*
  142. ===============
  143. Cam_MouseControl
  144. ===============
  145. */
  146. void Cam_MouseControl (float dtime)
  147. {
  148.     int        xl, xh;
  149.     int        yl, yh;
  150.     float    xf, yf;
  151.  
  152.     if (cambuttonstate != MK_RBUTTON)
  153.         return;
  154.  
  155.     xf = (float)(buttonx - camera.width/2) / (camera.width/2);
  156.     yf = (float)(buttony - camera.height/2) / (camera.height/2);
  157.  
  158.     xl = camera.width/3;
  159.     xh = xl*2;
  160.     yl = camera.height/3;
  161.     yh = yl*2;
  162.  
  163. #if 0
  164.     // strafe
  165.     if (buttony < yl && (buttonx < xl || buttonx > xh))
  166.         VectorMA (camera.origin, xf*dtime*MOVE_SPEED, camera.right, camera.origin);
  167.     else
  168. #endif
  169.     {
  170.         xf *= 1.0 - fabs(yf);
  171.         if (xf < 0)
  172.         {
  173.             xf += 0.1;
  174.             if (xf > 0)
  175.                 xf = 0;
  176.         }
  177.         else
  178.         {
  179.             xf -= 0.1;
  180.             if (xf < 0)
  181.                 xf = 0;
  182.         }
  183.         
  184.         VectorMA (camera.origin, yf*dtime*MOVE_SPEED, camera.forward, camera.origin);
  185.         camera.angles[YAW] += xf*-dtime*ANGLE_SPEED;
  186.     }
  187.     Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY);
  188. }
  189.  
  190.  
  191.  
  192.  
  193. /*
  194. ==============
  195. Cam_MouseDown
  196. ==============
  197. */
  198. void Cam_MouseDown (int x, int y, int buttons)
  199. {
  200.     vec3_t        dir;
  201.     float        f, r, u;
  202.     int            i;
  203.  
  204.     //
  205.     // calc ray direction
  206.     //
  207.     u = (float)(y - camera.height/2) / (camera.width/2);
  208.     r = (float)(x - camera.width/2) / (camera.width/2);
  209.     f = 1;
  210.  
  211.     for (i=0 ; i<3 ; i++)
  212.         dir[i] = camera.vpn[i] * f + camera.vright[i] * r + camera.vup[i] * u;
  213.     VectorNormalize (dir);
  214.  
  215.     Sys_GetCursorPos (&cursorx, &cursory);
  216.  
  217.     cambuttonstate = buttons;
  218.     buttonx = x;
  219.     buttony = y;
  220.  
  221.     // LBUTTON = manipulate selection
  222.     // shift-LBUTTON = select
  223.     // middle button = grab texture
  224.     // ctrl-middle button = set entire brush to texture
  225.     // ctrl-shift-middle button = set single face to texture
  226.     if ( (buttons == MK_LBUTTON)
  227.         || (buttons == (MK_LBUTTON | MK_SHIFT))
  228.         || (buttons == (MK_LBUTTON | MK_CONTROL))
  229.         || (buttons == (MK_LBUTTON | MK_CONTROL | MK_SHIFT))
  230.         || (buttons == MK_MBUTTON)
  231.         || (buttons == (MK_MBUTTON|MK_CONTROL))
  232.         || (buttons == (MK_MBUTTON|MK_SHIFT|MK_CONTROL)) )
  233.     {
  234.         Drag_Begin (x, y, buttons, 
  235.             camera.vright, camera.vup,
  236.             camera.origin, dir);
  237.         return;
  238.     }
  239.  
  240.     if (buttons == MK_RBUTTON)
  241.     {
  242.         Cam_MouseControl (0.1);
  243.         return;
  244.     }
  245. }
  246.  
  247. /*
  248. ==============
  249. Cam_MouseUp
  250. ==============
  251. */
  252. void Cam_MouseUp (int x, int y, int buttons)
  253. {
  254.     cambuttonstate = 0;
  255.     Drag_MouseUp ();
  256. }
  257.  
  258.  
  259. /*
  260. ==============
  261. Cam_MouseMoved
  262. ==============
  263. */
  264. void Cam_MouseMoved (int x, int y, int buttons)
  265. {
  266.     cambuttonstate = buttons;
  267.     if (!buttons)
  268.         return;
  269.     buttonx = x;
  270.     buttony = y;
  271.  
  272.     if (buttons == (MK_RBUTTON|MK_CONTROL) )
  273.     {
  274.         Cam_PositionDrag ();
  275.         Sys_UpdateWindows (W_XY|W_CAMERA|W_Z);
  276.         return;
  277.     }
  278.  
  279.     Sys_GetCursorPos (&cursorx, &cursory);
  280.  
  281.     if (buttons & (MK_LBUTTON | MK_MBUTTON) )
  282.     {
  283.         Drag_MouseMoved (x, y, buttons);
  284.         Sys_UpdateWindows (W_XY|W_CAMERA|W_Z);
  285.     }
  286. }
  287.  
  288.  
  289. vec3_t    cull1, cull2;
  290. int        cullv1[3], cullv2[3];
  291.  
  292. void InitCull (void)
  293. {
  294.     int        i;
  295.  
  296.     VectorSubtract (camera.vpn, camera.vright, cull1);
  297.     VectorAdd (camera.vpn, camera.vright, cull2);
  298.  
  299.     for (i=0 ; i<3 ; i++)
  300.     {
  301.         if (cull1[i] > 0)
  302.             cullv1[i] = 3+i;
  303.         else
  304.             cullv1[i] = i;
  305.         if (cull2[i] > 0)
  306.             cullv2[i] = 3+i;
  307.         else
  308.             cullv2[i] = i;
  309.     }
  310. }
  311.  
  312. qboolean CullBrush (brush_t *b)
  313. {
  314.     int        i;
  315.     vec3_t    point;
  316.     float    d;
  317.  
  318.     for (i=0 ; i<3 ; i++)
  319.         point[i] = b->mins[cullv1[i]] - camera.origin[i];
  320.  
  321.     d = DotProduct (point, cull1);
  322.     if (d < -1)
  323.         return true;
  324.  
  325.     for (i=0 ; i<3 ; i++)
  326.         point[i] = b->mins[cullv2[i]] - camera.origin[i];
  327.  
  328.     d = DotProduct (point, cull2);
  329.     if (d < -1)
  330.         return true;
  331.  
  332.     return false;
  333. }
  334.  
  335.  
  336. /*
  337. ==============
  338. Cam_Draw
  339. ==============
  340. */
  341. void Cam_Draw (void)
  342. {
  343.     brush_t    *brush;
  344.     face_t    *face;
  345.     float    screenaspect;
  346.     float    yfov;
  347.     double    start, end;
  348.     int        i;
  349.  
  350.     if (!active_brushes.next)
  351.         return;    // not valid yet
  352.  
  353.     if (camera.timing)
  354.         start = Sys_DoubleTime ();
  355.  
  356.     //
  357.     // clear
  358.     //
  359.     QE_CheckOpenGLForErrors();
  360.  
  361.     glViewport(0, 0, camera.width, camera.height);
  362.     glScissor(0, 0, camera.width, camera.height);
  363.     glClearColor (
  364.         g_qeglobals.d_savedinfo.colors[COLOR_CAMERABACK][0],
  365.         g_qeglobals.d_savedinfo.colors[COLOR_CAMERABACK][1],
  366.         g_qeglobals.d_savedinfo.colors[COLOR_CAMERABACK][2],
  367.         0);
  368.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  369.  
  370.     //
  371.     // set up viewpoint
  372.     //
  373.     glMatrixMode(GL_PROJECTION);
  374.     glLoadIdentity ();
  375.  
  376.     screenaspect = (float)camera.width/camera.height;
  377.     yfov = 2*atan((float)camera.height/camera.width)*180/Q_PI;
  378.     gluPerspective (yfov,  screenaspect,  2,  8192);
  379.  
  380.     glRotatef (-90,  1, 0, 0);        // put Z going up
  381.     glRotatef (90,  0, 0, 1);        // put Z going up
  382.     glRotatef (camera.angles[0],  0, 1, 0);
  383.     glRotatef (-camera.angles[1],  0, 0, 1);
  384.     glTranslatef (-camera.origin[0],  -camera.origin[1],  -camera.origin[2]);
  385.  
  386.     Cam_BuildMatrix ();
  387.  
  388.     InitCull ();
  389.  
  390.     //
  391.     // draw stuff
  392.     //
  393.  
  394.     switch (camera.draw_mode)
  395.     {
  396.     case cd_wire:
  397.         glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  398.         glDisable(GL_TEXTURE_2D);
  399.         glDisable(GL_TEXTURE_1D);
  400.         glDisable(GL_BLEND);
  401.         glDisable(GL_DEPTH_TEST);
  402.         glColor3f(1.0, 1.0, 1.0);
  403. //        glEnable (GL_LINE_SMOOTH);
  404.         break;
  405.  
  406.     case cd_solid:
  407.         glCullFace(GL_FRONT);
  408.         glEnable(GL_CULL_FACE);
  409.         glShadeModel (GL_FLAT);
  410.  
  411.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  412.         glDisable(GL_TEXTURE_2D);
  413.  
  414.         glDisable(GL_BLEND);
  415.         glEnable(GL_DEPTH_TEST);
  416.         glDepthFunc (GL_LEQUAL);
  417.         break;
  418.  
  419.     case cd_texture:
  420.         glCullFace(GL_FRONT);
  421.         glEnable(GL_CULL_FACE);
  422.  
  423.         glShadeModel (GL_FLAT);
  424.  
  425.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  426.         glEnable(GL_TEXTURE_2D);
  427.  
  428.         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  429.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  430.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  431.         glDisable(GL_BLEND);
  432.         glEnable(GL_DEPTH_TEST);
  433.         glDepthFunc (GL_LEQUAL);
  434.  
  435. #if 0
  436.  
  437.         {
  438.        GLfloat fogColor[4] = {0.0, 1.0, 0.0, 0.25};
  439.  
  440.         glFogi (GL_FOG_MODE, GL_LINEAR);
  441.         glHint (GL_FOG_HINT, GL_NICEST);  /*  per pixel   */
  442.         glFogf (GL_FOG_START, -8192);
  443.         glFogf (GL_FOG_END, 65536);
  444.        glFogfv (GL_FOG_COLOR, fogColor);
  445.  
  446.         }
  447.  
  448. #endif
  449.         break;
  450.  
  451.     case cd_blend:
  452.         glCullFace(GL_FRONT);
  453.         glEnable(GL_CULL_FACE);
  454.  
  455.         glShadeModel (GL_FLAT);
  456.  
  457.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  458.         glEnable(GL_TEXTURE_2D);
  459.  
  460.         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  461.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  462.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  463.         glDisable(GL_DEPTH_TEST);
  464.         glEnable (GL_BLEND);
  465.         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  466.         break;
  467.     }
  468.  
  469.     glMatrixMode(GL_TEXTURE);
  470.     for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)
  471.     {
  472.         if (CullBrush (brush))
  473.             continue;
  474.         if (FilterBrush (brush))
  475.             continue;
  476.  
  477.         Brush_Draw( brush );
  478.     }
  479.     glMatrixMode(GL_PROJECTION);
  480.  
  481.     //
  482.     // now draw selected brushes
  483.     //
  484.  
  485.     glTranslatef (g_qeglobals.d_select_translate[0], g_qeglobals.d_select_translate[1], g_qeglobals.d_select_translate[2]);
  486.     glMatrixMode(GL_TEXTURE);
  487.  
  488.     // draw normally
  489.     for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  490.     {
  491.         Brush_Draw( brush );
  492.     }
  493.  
  494.     // blend on top
  495.     glMatrixMode(GL_PROJECTION);
  496.  
  497.     glColor4f(1.0, 0.0, 0.0, 0.3);
  498.     glEnable (GL_BLEND);
  499.     glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  500.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  501.     glDisable (GL_TEXTURE_2D);
  502.     for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  503.         for (face=brush->brush_faces ; face ; face=face->next)
  504.             Face_Draw( face );
  505.     if (selected_face)
  506.         Face_Draw(selected_face);
  507.  
  508.     // non-zbuffered outline
  509.  
  510.     glDisable (GL_BLEND);
  511.     glDisable (GL_DEPTH_TEST);
  512.     glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  513.     glColor3f (1, 1, 1);
  514.     for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  515.         for (face=brush->brush_faces ; face ; face=face->next)
  516.             Face_Draw( face );
  517.  
  518.     // edge / vertex flags
  519.  
  520.     if (g_qeglobals.d_select_mode == sel_vertex)
  521.     {
  522.         glPointSize (4);
  523.         glColor3f (0,1,0);
  524.         glBegin (GL_POINTS);
  525.         for (i=0 ; i<g_qeglobals.d_numpoints ; i++)
  526.             glVertex3fv (g_qeglobals.d_points[i]);
  527.         glEnd ();
  528.         glPointSize (1);
  529.     }
  530.     else if (g_qeglobals.d_select_mode == sel_edge)
  531.     {
  532.         float    *v1, *v2;
  533.  
  534.         glPointSize (4);
  535.         glColor3f (0,0,1);
  536.         glBegin (GL_POINTS);
  537.         for (i=0 ; i<g_qeglobals.d_numedges ; i++)
  538.         {
  539.             v1 = g_qeglobals.d_points[g_qeglobals.d_edges[i].p1];
  540.             v2 = g_qeglobals.d_points[g_qeglobals.d_edges[i].p2];
  541.             glVertex3f ( (v1[0]+v2[0])*0.5,(v1[1]+v2[1])*0.5,(v1[2]+v2[2])*0.5);
  542.         }
  543.         glEnd ();
  544.         glPointSize (1);
  545.     }
  546.  
  547.     //
  548.     // draw pointfile
  549.     //
  550.     glEnable(GL_DEPTH_TEST);
  551.  
  552.     DrawPathLines ();
  553.  
  554.     if (g_qeglobals.d_pointfile_display_list)
  555.     {
  556.         Pointfile_Draw();
  557. //        glCallList (g_qeglobals.d_pointfile_display_list);
  558.     }
  559.  
  560.     // bind back to the default texture so that we don't have problems
  561.     // elsewhere using/modifying texture maps between contexts
  562.     glBindTexture( GL_TEXTURE_2D, 0 );
  563.  
  564.     glFinish();
  565.     QE_CheckOpenGLForErrors();
  566. //    Sys_EndWait();
  567.     if (camera.timing)
  568.     {
  569.         end = Sys_DoubleTime ();
  570.         Sys_Printf ("Camera: %i ms\n", (int)(1000*(end-start)));
  571.     }
  572. }
  573.  
  574.